package yxy.game.pm2.command.soul;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;

/** 魂玉 魂玉替换 */
@MessageType(proto = Command.SoulChange, classtype = HeroMessage.SoulChangeRequest.class)
public class CMD_SoulChange extends Command<HeroMessage.SoulChangeRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.SoulChangeRequest request) {
		String playerCode = messageSource.getTag();
		String heroCode = request.getHero();
		int pos = request.getPos();
		String soulCode = request.getCode();

		HeroMessage.SoulChangeResponse.Builder response = HeroMessage.SoulChangeResponse.newBuilder();

		try {
			if (Hero.lockHero(playerCode) && Soul.lockSoul(playerCode)) {

				// 条件判断===========================================================
				Hero hero = Hero.get(playerCode, heroCode);
				if (hero == null) {
					send(messageSource, response, Result.ParamError);
					return;
				}

				String heroSoulCode = null;
				heroSoulCode = pos == 0 ? hero.getSoulA() : hero.getSoulB();
				herodata _herodata = DataCenter.getData(hero.getDataId(), herodata.class);
				int soulposition = 0;
				int soulasever = 0;
				switch (pos) {
				case 0: {
					heroSoulCode = hero.getSoulA();
					soulposition = _herodata.getSoulposition1();
					soulasever = _herodata.getSoulasever1();
				}
					break;
				case 1: {
					heroSoulCode = hero.getSoulB();
					soulposition = _herodata.getSoulposition2();
					soulasever = _herodata.getSoulasever2();
				}
					break;
				default:
					send(messageSource, response, Result.ParamError);
					return;
				}
				// 判断魂玉槽开启状态
				if (soulposition == 0) {
					send(messageSource, response, Result.ParamError);
					return;
				}
				// 判断魂玉槽开启条件
				switch (soulposition) {
				case 1:// 武将等级
					if (soulasever > hero.getLevel()) {
						send(messageSource, response, Result.SoulChangeLowLevel);// 武将等级不足
						return;
					}
					break;
				case 2:// 武将星级
					if (soulasever > hero.getStar()) {
						send(messageSource, response, Result.SoulChangeLowStar);// 武将星级不足
						return;
					}
					break;
				}

				// 操作===========================================================
				if (heroSoulCode != null) {
					// 卸下
					if (pos == 0) {
						hero.setSoulA(null);
					} else {
						hero.setSoulB(null);
					}
					Soul heroSoul = Soul.get(playerCode, heroSoulCode);
					if (heroSoul != null) {
						heroSoul.setHero(null);
						Soul.set(playerCode, heroSoul);
					}
				}
				if (soulCode != null) {
					Soul soul = Soul.get(playerCode, soulCode);
					if (soul != null) {
						if (pos == 0) {
							hero.setSoulA(soul.getCode());
						} else {
							hero.setSoulB(soul.getCode());
						}
						soul.setHero(hero.getCode());
						Soul.set(playerCode, soul);
					}
				}

				new HeroHandle().updateHero(playerCode, hero);

				// 封消息===========================================================
				response.setInfo(new HeroHandle().getHeroInfo(playerCode, hero));
				send(messageSource, response, Result.Success);

			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			Hero.unlockHero(playerCode);
			Soul.unlockSoul(playerCode);
		}
	}

	private void send(MessageSource messageSource, HeroMessage.SoulChangeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}